<?php
/**
 * WpTabTextView.php
 * 
 * This file contains the class WpTabTextView, a view for WildPack mvc 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage mvc.view
 */

/**
 * The interface to implement
 */
require_once (dirname(__FILE__).'/IWpView.php');

/**
 * This view tries to convert the provided data into tabulated text. 
 * @author Robert Schmidt <robert@wildpack.de>
 * @version 1.0
 * @package wildpack
 * @subpackage mvc.view
 */
class WpTabTextView
{
	public $Data = '';
	private $assignmenttype = 'rowarray';
	private $nlreplacement = "\\n";
	
	/**
	 * The class constructor
	 * @param array $config The configurations object
	 * @cfg assignmenttype Describes how data will be assign must be on of the following:
	 * "rowarray" = Each assignment is a row provided as array (This is the default), 
	 * "rowstring" = Each assignment is a row already formatted as tabulated string, 
	 * "multirowarray" = Each assignment is an array of row arrays,
	 * "multirowstring" = Each assignment is an array of already formatted row strings,
	 * "tablestring" = Each assignment is already complete formatted and can be used without processing 
	 * @cfg nlreplacement The string to be used to replace CR chars in data. A CR marks the end of a row, so CR 
	 * within data will cause an unwanted row end and therefore needs to be replaced
	 */
	public function __construct($config = null)
	{
		if (is_null($config)) {
			$config = array();
		}
		
		$this->assignmenttype = array_key_exists('assignmenttype', $config) ? $config['assignmenttype'] : $this->assignmenttype; 
		$this->nlreplacement = array_key_exists('nlreplacement', $config) ? $config['nlreplacement'] : $this->nlreplacement;
	}
	
	/**
   * Assigns data to the view
   * @param mixed $data The data to assign to the view
   */
	public function assign($data)
	{
		if ($this->assignmenttype === 'rowstring' || $this->assignmenttype === 'tablestring') {
			$this->Data .= str_replace("\n", $this->nlreplacement, strval($data))."\n";
		}
		
		if ($this->assignmenttype === 'rowarray' && is_array($data)) {
			$this->Data .=  str_replace("\n", $this->nlreplacement, implode("\t", $data))."\n";
		}
		
		if ($this->assignmenttype === 'multirowarray' || $this->assignmenttype === 'multirowstring') {
			if(is_array($data)) {
				foreach ($data as $row) {
					if ($this->assignmenttype === 'multirowstring') {
						$this->Data .=  str_replace("\n", $this->nlreplacement, strval($row))."\n";
					}
					
					if ($this->assignmenttype === 'multirowarray' && is_array($row)) {
							$this->Data .=  str_replace("\n", $this->nlreplacement, implode("\t", $row))."\n";
					}
				}
			}
		}
	}
	
	/**
   * Renders the view and returns the content
   * @param object $viewname[optional] The name of the view to render
   * @return string The rendered view as string
   */
	public function fetch($viewname = '')
	{
		return $this->Data;
	}
	
	/**
   * Renders the view
   * @param string $viewname The name of the view to render
   * @param object $response The response to write the rendered view to
   */
	public function render($response)
	{
		$response->write($this->Data);
		$response->flush();
		$this->Data = '';
	}
	
	/**
   * Specifies the name of the view to use
   * @param string $viewname The name of the view to use
   */
	public function setViewname($viewname) {}
}

/* End of file WpTabTextView.php */
/* Location: ./wildpack/mvc/view/WpTabTextView.php */
